package xx.services.entity.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.bas.Picture;
import xx.services.Passport;
import xx.services.entity.inter.IPictureInterface;
import xx.services.util.Constants;
import xx.util.Arr;

/**
 * 实体图片服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/picture")
 * @author Administrator
 * 
 * 2010-12-19
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/picture")
public class PictureRest extends Passport implements IPictureInterface
{

    public PictureRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 删除图片{picture_id}集。
     * 
     * @DELETE
     * @Path ("/{picture_id}")
     * @param pictureId-@PathParam("picture_id")
     *            图片ID，多个图片ID用“_”隔开
     * @return
     * @author dengjinye,2010-10-29
     */
    @DELETE
    @Path("/{picture_id}")
    public String deletePicture(@PathParam("picture_id")
    String pictureId)
    {
        try
        {
            long[] pictureArr = Arr.toLongArray(pictureId, "_");
            for (long id : pictureArr)
            {
                Picture picture = new Picture(qr, owner);
                picture.setPictureId(id);
                picture.delete();
            }
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 修改图片{picture_id}的名称、描述等。
     * 
     * @PUT
     * @Path ("/{picture_id}")
     * @param pictureId-@PathParam("picture_id")
     *            图片ID
     * @param name-@FormParam("name")
     *            图片名称
     * @param description-@FormParam("description")
     *            图片描述
     * @return 新图片实体
     * @author dengjinye,2010-10-29
     */
    @PUT
    @Path("/{picture_id}")
    public String updatePicture(@PathParam("picture_id")
    long pictureId, @FormParam("name")
    String name, @FormParam("description")
    String description)
    {
        try
        {
            Picture picture = new Picture(qr, owner);
            picture.setPictureId(pictureId).setName(name).setDescription(description);
            picture.update(Picture.NAME, Picture.DESCRIPTION);
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

}
